// components/energy-ranking/energy-ranking.js
const app = getApp();
Component({
  properties: {
    userList: {
      type: Array,
      value: [],
    },
  },

  data: {
    StatusBar: app.globalData.StatusBar, 
    capsuleBottom: app.globalData.capsuleBottom, // 胶囊底部加20rpx的偏移量
    topUsers: [],
    otherUsers: [],
  },

  observers: {
    'userList': function (userList) {
      this.sortAndSetData(userList);
    }
  },

  methods: {
    sortAndSetData(userList) {
      const sortedList = userList.slice().sort((a, b) => {
        if (b.energy === a.energy) return a.id - b.id;
        return b.energy - a.energy;
      });

      this.setData({
        topUsers: sortedList.slice(0, 3),
        otherUsers: sortedList.slice(3),
      });
    },
  }
});
